﻿using System;
using System.Text;
using System.Runtime.InteropServices;
using Spire.Xls;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;

namespace WinFormsApp1
{
    public class ReadCard
    {
        static int[] parg0 = new int[] { 0};
        static int[] parg1 = new int[] { 8811};
        static int[] parg2 = new int[] { 9986};


        [DllImport("RdCard.dll")]
        private static extern int UCommand1(byte[] pCmd,int[] parg0,int[] parg1,int[] parg2);

        [DllImport("RdCard.dll")]
        private static extern int GetName(byte[] buf);

        [DllImport("RdCard.dll")]
        private static extern int GetSexGB(byte[] buf);

        [DllImport("RdCard.dll")]
        private static extern int GetFolkGB(byte[] buf);

        [DllImport("RdCard.dll")]
        private static extern int GetBirth(byte[] buf);

        [DllImport("RdCard.dll")]
        private static extern int GetAddr(byte[] buf);

        [DllImport("RdCard.dll")]
        private static extern int GetIDNum(byte[] buf);

        [DllImport("RdCard.dll")]
        private static extern int GetDep(byte[] buf);

        [DllImport("RdCard.dll")]
        private static extern int GetBegin(byte[] buf);

        [DllImport("RdCard.dll")]
        private static extern int GetEnd(byte[] buf);

        [DllImport("RdCard.dll")]
        private static extern int GetBmpPath(byte[] buf);



        private static string byte2str(byte[] param) {
            string str = Encoding.GetEncoding("GB2312").GetString(param).Trim().Replace(" ", "").TrimEnd('\0');
            return str;
        }
        private static void initCom() {
            byte[] cmd = new byte[] { 0x41 };
            int reb = UCommand1(cmd, parg0, parg1, parg2);
           
        }

        public static bool CloseCom()
        {
           
            try
            {
                byte[] cmd = new byte[] { 0x42 };
                int nRet = UCommand1(cmd, parg0, parg1, parg2);
                if (nRet == 62171)
                {
                    //sMsg = "端口关闭成功";
                    return true;
                }
                else
                { 
                }
            }
            catch (Exception ex)
            {
               // sMsg = "端口关闭失败,原因是:" + ex.Message;
            }
            return false;
        }

        private static void write(string str)
        {
            System.Diagnostics.Debug.WriteLine(str);
        }

        public static CardInfo readCardInfo()
        {
            CardInfo objCardInfo = new CardInfo();
            //Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            initCom();
            byte[] cmd1 = new byte[] { 0x43 };
            int reb1 = UCommand1(cmd1, parg0, parg1, parg2);
  
            byte[] cmd2 = new byte[] { 0x44 };
            int ret = UCommand1(cmd2, parg0, parg1, parg2);


            
            if (ret == 62171 || ret == 62172 || ret == 62173 || ret == 62174)
            {

                
                switch (ret)
                {
                    case 62171:
                        objCardInfo.code = 1;
                        byte[] param = new byte[100];
                        GetName(param);
                        string str = byte2str(param);
                        objCardInfo.Name = str;
                        str = "";

                        param = new byte[100];
                        GetAddr(param);
                        str = byte2str(param);
                        objCardInfo.Address = str;
                        str = "";

                        param = new byte[100];
                        GetBirth(param);
                        str = byte2str(param);
                        objCardInfo.Birthday = str;
                        str = "";

                        param = new byte[100];
                        GetSexGB(param);
                        str = byte2str(param);
                        objCardInfo.Sex = str;
                        str = "";

                        param = new byte[100];
                        GetFolkGB(param);
                        str = byte2str(param);
                        objCardInfo.Nation = str;
                        str = "";

                        param = new byte[100];
                        GetIDNum(param);
                        str = byte2str(param);
                        objCardInfo.Id = str;
                        str = "";

                        //签发机关
                        param = new byte[100];
                        GetDep(param);
                        str = byte2str(param);
                        objCardInfo.Department = str;
                        str = "";

                        param = new byte[100];
                        GetBegin(param);
                        str = byte2str(param);
                        objCardInfo.StartDate = str;
                        str = "";

                        param = new byte[100];
                        GetEnd(param);
                        str = byte2str(param);
                        objCardInfo.EndDate = str;
                        str = "";

                        param = new byte[100];
                        GetBmpPath(param);
                        str = byte2str(param);
                        objCardInfo.PhotoPath = str;
                        str = "";
                        break;
                }
                
                return objCardInfo;
            }
            else
            {
                MessageBox.Show("读卡失败，请放置卡片！", "提示", MessageBoxButtons.OK);
                return null;
            }

            
        }

        public static void excelData(CardInfo objCardInfo) {
            try
            {
                string importExcelPath = "BaseForm.xlsx";
                string exportExcelPath = "export.xlsx";

                Spire.Xls.Workbook workbook = new Spire.Xls.Workbook();
                workbook.LoadFromFile(importExcelPath);
                Spire.Xls.Worksheet sheet = workbook.Worksheets[0];
                sheet.Range[3, 3].Text = objCardInfo.Name;//name
                sheet.Range[3, 5].Text = objCardInfo.Sex;//sex
                sheet.Range[3, 7].Text = objCardInfo.dealBri();//bri
                sheet.Range[4, 3].Text = objCardInfo.Nation;//民族
                sheet.Range[5, 3].Text = objCardInfo.Id;//ID
                sheet.Range[5, 9].Text = objCardInfo.dealDate();//有效期
                sheet.Range[6, 3].Text = objCardInfo.Address;//地址
                sheet.Range[7, 3].Text = objCardInfo.Address;//地址
                sheet.Range[19, 3].Text = objCardInfo.Address;//地址
                objCardInfo.code = 1;
                workbook.SaveToFile(exportExcelPath);

            }
            catch(Exception e)
            {
                MessageBox.Show(""+e.ToString(), "写入EXCLE文件失败", MessageBoxButtons.OK);
            }
            //finally
            //{
            //    MessageBox.Show("", "提示", MessageBoxButtons.OK);
            //
            
        }

        

        public static bool findIP()
        {
            string name = Dns.GetHostName();
            IPAddress[] ipadrlist = Dns.GetHostAddresses(name);
            bool flag = false;
            foreach (IPAddress ipa in ipadrlist)
            {
                if (ipa.AddressFamily == AddressFamily.InterNetwork)
                {
                    string[] iplist = ipa.ToString().Split(new char[1] { '.' });
                    if (iplist[1] == "69")
                        flag = true;

                }
            }
            return flag;
        }

    }

  
   

    /// <summary>
    /// 身份证的信息
    /// </summary>
    public class CardInfo
    {

        public string Id;
        /// <summary>
        /// 身份证号码
        /// </summary>
        public string CardNo;
        /// <summary>
        /// 姓名
        /// </summary>
        public string Name;
        /// <summary>
        /// 性别
        /// </summary>
        public string Sex;
        /// <summary>
        /// 出生日期
        /// </summary>
        public string Birthday;
        /// <summary>
        /// 地址
        /// </summary>
        public string Address;

        /// <summary>
        /// 发卡机关
        /// </summary>
        public string Department;
        /// <summary>
        /// 证件开始日期
        /// </summary>
        public string StartDate;
        /// <summary>
        /// 证件结束日期
        /// </summary>
        public string EndDate;
        /// <summary>
        /// 民族
        /// </summary>
        public string Nation;
        /// <summary>
        /// 相片路径
        /// </summary>
        public string PhotoPath;
        public int code;

        public string dealBri() {
            string str = this.Birthday.Insert(4,".").Insert(7,".");
            return str;
        }

        public string dealDate()
        {
           string start =  this.StartDate.Insert(4, ".").Insert(7, ".");
            if (EndDate.Length < 3)
            {
                return start + "-" + EndDate;
            }
            else
            {
                string end = this.EndDate.Insert(4, ".").Insert(7, ".");
                return start + "-" + end;
            }

            
        }
    }
        
   
}
